package com.jiuyuan.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiuyuan.entity.Employee;
import com.jiuyuan.entity.MailConstant;
import com.jiuyuan.entity.MailLog;
import com.jiuyuan.entity.vo.RespBean;
import com.jiuyuan.entity.vo.RespPageBean;
import com.jiuyuan.mapper.EmployeeMapper;
import com.jiuyuan.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiuyuan.service.IMailLogService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiuyuan
 * @since 2023-02-15
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    IMailLogService mailLogService;

    @Override
    public RespPageBean allList(Integer currentPage, Integer pageSize, Employee employee, LocalDateTime[] localDateTimes) {
        IPage<Employee> page = new Page<>(currentPage, pageSize);
        employeeMapper.allList(page,employee,localDateTimes);
        return new RespPageBean(page.getTotal(),page.getRecords());
    }

    @Override
    public Integer maxWorkId() {
        return employeeMapper.maxWorkId()+1;

    }

    @Override
    public RespBean add(Employee employee) {
        //处理合同期限，获取天数，进行格式化
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days/365.00)));
        int insert = employeeMapper.insert(employee);
        if(1 == insert){

            Employee emp = employeeMapper.getEmployeeById(employee.getId()).get(0);

            //插入消息表
            //生成随机确认Id
//            String msgID = UUID.randomUUID().toString();
            String msgID = "123456";
            MailLog mailLog = new MailLog();
            mailLog.setMsgId(msgID);
            mailLog.setEid(emp.getId());
            mailLog.setStatus(MailConstant.DELEVERING);
            mailLog.setRouteKey(MailConstant.MAIL_ROUTING_KEY_NAME);
            mailLog.setExchange(MailConstant.MAIL_EXCHANGE_NAME);
            mailLog.setCount(0);
            mailLog.setTryTime(LocalDateTime.now().plusMinutes(MailConstant.MAX_TRY_TIME));
            mailLog.setCreateTime(LocalDateTime.now());
            mailLog.setUpdateTime(LocalDateTime.now());
            mailLogService.save(mailLog);

            //发送消息
            rabbitTemplate.convertAndSend(MailConstant.MAIL_EXCHANGE_NAME,MailConstant.MAIL_ROUTING_KEY_NAME,emp,new CorrelationData(msgID));



            return RespBean.success("插入成功");
        }
        return RespBean.error("插入失败");
    }

    @Override
    public List<Employee> getEmployeeById(Integer id) {
        return employeeMapper.getEmployeeById(id);
    }

    @Override
    public RespPageBean employeeWithSaraly(Integer currentPage, Integer pageSize) {
        Page<Employee> page = new Page<>(currentPage, pageSize);
        IPage<Employee> employeeWithSaraly = employeeMapper.employeeWithSaraly(page);
        return new RespPageBean(employeeWithSaraly.getTotal(),employeeWithSaraly.getRecords());
    }
}
